﻿@if (isReady)
{
    <MDCard Outline="Outline" Class="h-100" Style="display: flex; justify-content: center; flex-direction: column; ">
        @if (isRuning)
        {
            <div class="mb-4" style=" display: flex; justify-content: center;">
                正在同步评分......
            </div>
        }
        else
        {
            @if (Model.UserScores.Any(s => s.Socres.IsScored))
            {
                <div class="pb-4">
                    <CnGalWebSite.Shared.MasaComponent.Shared.Components.Charts.MasaRadarChart Model="model" MaxHeight="300" MaxWidth="270" />
                </div>

            }
            else
            {
                <div class="mb-4" style=" display: flex; justify-content: center;">
                    暂无记录
                </div>
            }
        }
    </MDCard>
}



@code {
    [Parameter]
    public bool Outline { get; set; }

    [Parameter]
    public PlayedGameOverviewModel Model { get; set; }

    EChartsRadarOptionModel model = new EChartsRadarOptionModel();
    bool isReady;
    bool isRuning;
    protected override void OnParametersSet()
    {
        isRuning = false;
        if (Model.UserScores.Any(s => s.Socres.IsScored) == false)
        {
            isReady = true;
            return;
        }

        model.Legend.Data.Clear();
        if (model.Series.Any() == false)
        {
            model.Series.Add(new EChartsRadarOptionSery
            {
                Name = "评分"
            });

        }
        var series = model.Series.First().Data;
        series.Clear();
        if(Model.GameTotalScores.IsScored)
        {
            model.Legend.Data.Add("所有");
            series.Add(new EChartsRadarOptionSeryData
            {
                Name = "所有",
                Value = new List<double>
                {
                    Math.Round(Model.GameTotalScores.SystemSocre, 2), Math.Round(Model.GameTotalScores.CVSocre, 2), Math.Round( Model.GameTotalScores.ScriptSocre, 2), Math.Round( Model.GameTotalScores.ShowSocre, 2),  Math.Round(Model.GameTotalScores.PaintSocre, 2),  Math.Round(Model.GameTotalScores.MusicSocre, 2)
                }
            });
        }
        if (Model.GameReviewsScores.IsScored)
        {
            model.Legend.Data.Add("过滤后");

            series.Add(new EChartsRadarOptionSeryData
            {
                Name = "过滤后",
                Value = new List<double>
                {
                    Math.Round(Model.GameReviewsScores.SystemSocre, 2),  Math.Round(Model.GameReviewsScores.CVSocre, 2),  Math.Round(Model.GameReviewsScores.ScriptSocre, 2), Math.Round( Model.GameReviewsScores.ShowSocre, 2),  Math.Round(Model.GameReviewsScores.PaintSocre, 2), Math.Round( Model.GameReviewsScores.MusicSocre, 2)
                }
            });
        }



        var myscores = Model.UserScores.FirstOrDefault(s => s.User.Id == Model.CurrentUserId && s.Socres.IsScored);
        if (myscores != null)
        {
            model.Legend.Data.Add("我的");
            series.Add(new EChartsRadarOptionSeryData
            {
                Name = "我的",
                Value = new List<double>
            {
                    Math.Round(myscores.Socres.SystemSocre, 2), Math.Round(myscores.Socres.CVSocre, 2), Math.Round(myscores.Socres.ScriptSocre, 2),Math.Round( myscores.Socres.ShowSocre, 2), Math.Round(myscores.Socres.PaintSocre, 2), Math.Round(myscores.Socres.MusicSocre, 2)
                }
            });
        }

        //计算最大值
        if(series.Any(s=>s.Value.Any())==false)
        {
            isReady = true;
            isRuning = true;
            return;
        }
        var min = series.Min(s => s.Value.Min());
        var max = series.Max(s => s.Value.Max());

        model.Radar.Indicator.Clear();

        model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
        {
            Name = "程序"
        });
        if (Model.IsDubbing)
        {
            model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
            {
                Name = "配音"
            });
        }
        model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
        {
            Name = "剧本"
        });
        model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
        {
            Name = "演出"
        });
        model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
        {
            Name = "美术"
        });

        model.Radar.Indicator.Add(new EChartsRadarOptionIndicator
        {
            Name = "音乐"
        });

        foreach (var item in model.Radar.Indicator)
        {
            item.Min = min - 1 < 0 ? 0 : min - 1;
            item.Max = max + 0.1;
        }
        if (Model.IsDubbing == false)
        {
            series[0].Value.RemoveAt(1);
            if(series.Count>1)
            {
                series[1].Value.RemoveAt(1);
            }
            if (series.Count > 2)
            {
                series[2].Value.RemoveAt(1);
            }
        }

        isReady = true;
    }

}
